package com.fanjia.taobao.common.util;


import com.fanjia.taobao.common.exceptionhandler.MyException;

import java.util.regex.Pattern;

/**
 * @author fanjia
 * @description 校验工具类
 * @date  2021/4/9 9:49
 * @param
 * @return
 **/
public class ValidatorUtil {
    /**
     * 正则表达式：验证用户名
     */
    public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,20}$";

    /**
     * 正则表达式：验证密码
     */
    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$";

    /**
     * 正则表达式：验证手机号
     */
    public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";

    /**
     * 正则表达式：验证邮箱
     */
    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";


    /**
     * 校验用户名
     *
     * @param username
     * @return
     */
    public static void isUsername(String username) {
        if(!Pattern.matches(REGEX_USERNAME, username)){
            throw new MyException("用户名不规范，需要字母开头，6-21位");
        }
    }

    /**
     * 校验密码
     *
     * @param password
     * @return
     */
    public static void isPassword(String password) {

        if(!Pattern.matches(REGEX_PASSWORD, password)){
            throw new MyException("密码不规范，需要字母或数字开头，6-20位");
        }
    }

    /**
     * 校验手机号
     *
     * @param mobile
     * @return
     */
    public static void isMobile(String mobile) {
        if(!Pattern.matches(REGEX_MOBILE, mobile)){
            throw new MyException("手机号不规范");
        }
    }

    /**
     * 校验邮箱
     *
     * @param email
     * @return
     */
    public static void isEmail(String email) {
        if(!Pattern.matches(REGEX_EMAIL, email)){
            throw new MyException("邮箱不规范");
        }
    }
}